//
//  PushTransitionAnimation.m
//  29-HomeWork_TransitionAnimation
//
//  Created by dllo on 16/8/23.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "PushTransitionAnimation.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "MyTableViewCell.h"

@implementation PushTransitionAnimation
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.5f;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    // 获取toVC, fromVC, containerView
    ViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    SecondViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    // 获取当前的cell
    MyTableViewCell *cell = [fromVC.tableView cellForRowAtIndexPath:fromVC.currentIndexPath];
    // 隐藏图片
    toVC.toImageView.hidden = YES;
    [containerView addSubview:toVC.view];
    // 截图
    UIView *snpView = [cell snapshotViewAfterScreenUpdates:NO];
    // 计算出此时cell的imageView相对坐标
    CGRect newFrame = [fromVC.view convertRect:cell.imageOfCell.frame fromView:cell.contentView];
    snpView.frame = newFrame;
    [containerView addSubview:snpView];
    
//    // 改变label优先级
//    toVC.layoutOfLabelTop0.priority = 751;
//    toVC.layoutOflabelTop450.priority = 750;
    
    // 动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        [containerView layoutIfNeeded];
        snpView.frame = toVC.toImageView.frame;
//        [toVC.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        toVC.toImageView.hidden = NO;
        [snpView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
    
}

@end
